package juc.callable;

import java.util.concurrent.*;

public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        //创建一个固定大小的线程池
        ExecutorService pool = Executors.newFixedThreadPool(1);
        // 向线程池提交一个线程任务，这个线程任务返回一句话
        Future<String> future = pool.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(3000L);
                return "科大讯飞是中国最大的语音提供商";
            }

        });
        System.out.println("我喜欢tt");
        //获取线程执行的结果  阻塞线程，直到有返回结果
        System.out.println(future.get());

        MyTask task = new MyTask();
        Future<String> futrue2 = pool.submit(task);
        //取消线程任务
        futrue2.cancel(true);
        System.out.println("任务已经取消：" + futrue2.isCancelled());
        System.out.println("返回结果======" + futrue2.get());
        System.out.println(future.get(1, TimeUnit.SECONDS));
    }

    public static class MyTask implements Callable<String> {
        @Override
        public String call() throws Exception {

            return "科大讯飞是中国最大的语音提供商";
        }

    }

}